﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClearCanvas.Dicom;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace XMLGenerate
{
    public partial class Form1 : Form
    {
        private string fileName = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog flg = new OpenFileDialog();
            flg.Filter = "Dicom File | *.dcm | All Files | *.*";
            flg.Multiselect = false;
            if (flg.ShowDialog()==DialogResult.OK)
            {
                fileName = flg.FileName;
                textBox1.Text = fileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Generate
            DicomFile ff = new DicomFile();
            ff.Load(DicomReadOptions.Default, fileName);

            XmlGenerate generate = new XmlGenerate();
            generate.WriteDicomAttributeCollection(null, ff.DataSet);
            generate.WriteXMLFile("test.xml");
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var doc=XDocument.Load("test.xml");
            try
            {
                var list = 
                    from d in (from c in doc.Descendants("Sequence") where c.Attribute("Tag").Value=="300A0010" select c).Descendants("DicomTag")
                    where d.Attribute("Tag").Value=="300A0012" select d;
                //XDocument d = new XDocument(list);

//                var item = from c in d.Descendants("SequenceItem") select c;

  //              IEnumerable<XElement> dicomtag = from c in (new XDocument(item)).Descendants("DicomTag") where c.Attribute("Tag").Value=="300A0012" select c;
                
                foreach (XElement ele in list)
                {
                    ele.SetValue("10");
                }
                doc.Save("test.xml");
               //select new
               //{                   
               //    id = c.Element("id").Value,
               //    tag = c.Element("Tag").Value,
               //    TagName = c.Element("TagName").Value,
               //    TagValue = c.Element("Value").Value
               //};
                                          
            }
            catch (System.Exception ex)
            {
            	
            }

            

        }
    }
    public class dicomTag
    {
        public String id;
        public String tag;
        public String TagName;
        public String TagValue;
    }
}
